﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;

public partial class Admin_adm_subcat_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                this._Binddropdown();
                //Truyen param vao
                if (Request.Params["ctid"] != null && Request.Params["scn"] != null)
                {
                    a_CatTree act = a_CatTreeService.Geta_CatTree(int.Parse(Request.Params["ctid"]));
                    if (act != null)
                    {
                        this.drpMainCategory.SelectedValue = act.CatID.ToString();
                    }
                    a_CatSub acs = a_CatSubService.Geta_CatSub(act.SubCatID);
                    if (acs != null)
                    {
                        this.txtOrdering.Text = acs.Ordering.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                //throw new error
            }
        }
    }

    private void _Binddropdown()
    {
        this.drpMainCategory.DataSource = a_CatMainService.Geta_CatMainList(a_CatMainColumns.UName, OrderDirection.ASC);
        this.drpMainCategory.DataTextField = a_CatMainColumns.UName.ToString();
        this.drpMainCategory.DataValueField = a_CatMainColumns.CatID.ToString();
        this.drpMainCategory.DataBind();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int oldorder = 0;
        int neworder = 0;
        try
        {
            neworder = int.Parse(this.txtOrdering.Text);
        }
        catch (Exception ex)
        {
        }
        a_CatTree act = a_CatTreeService.Geta_CatTree(int.Parse( Request.Params["ctid"]));
        if (act != null)
        {
            a_CatTreeService.Deletea_CatTree(int.Parse(Request.Params["ctid"]));
            //Them vao tree
            act.CatID = int.Parse(this.drpMainCategory.SelectedValue);
            a_CatTreeService.Createa_CatTree(act);
        }
        a_CatSub acs = a_CatSubService.Geta_CatSub(act.SubCatID);
        if (acs != null)
        {
            //oldorder = acs.Ordering;
            //acs.Ordering = neworder;
            //a_CatSubService.Updatea_CatSub(acs);
          UpdateOrder(acs, neworder);
        }

        //a_CatSubCollection ascoll = a_CatSubService.Geta_CatSubList_mcid(acs.MainCatID,a_CatSubColumns.Ordering,OrderDirection.ASC);
        //a_CatSubCollection aSub = new a_CatSubCollection();

        //foreach (a_CatSub aItem in ascoll)
        //{
        //    aSub.Add(aItem);
        //}
        //int iStart = 0;
        //a_CatSubCollection aRemove = new a_CatSubCollection();
        //foreach (a_CatSub acsi in aSub)
        //{
        //    if (acsi.Ordering == neworder)
        //    {
        //        aRemove.Add(acsi);
        //        if (acs.SubCatID == acsi.SubCatID)
        //        {
        //            ascoll.RemoveAt(iStart);
        //        }
        //    }
        //    iStart++;
        //}
        //foreach (a_CatSub asi in aRemove)
        //{
        //    if (asi.SubCatID != acs.SubCatID)
        //    {
        //        asi.Ordering += 1;
        //    }
        //    a_CatSubService.Updatea_CatSub(asi);
        //}

        //for (int i = 0; i < ascoll.Count; i++)
        //{
        //    ascoll[i].Ordering = ascoll[i].Ordering + 2;
        //    a_CatSubService.Updatea_CatSub(ascoll[i]);
        //}
        
        this.lblAlert.Text = "Update order successfully!";
        this.lblAlert.Visible = true;
    }
    public void UpdateOrder(a_CatSub cat, int order)
    {
      //cat.Ordering = order;
      //a_CatSub cat2 = a_CatSubService.Geta_CatSubByOrder(order, cat.MainCatID);
      //a_CatSubService.Updatea_CatSub(cat);
      //if (cat2 != null && order != 0)
      //{
      //  UpdateOrder(cat2, order + 1);
      //}
    }
}
